package com.sskj.motherboard.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sskj.common.constant.RabbitMQConstant;
import com.sskj.common.dto.DeviceQueueMessage;
import com.sskj.common.enums.DeviceMessageTypeEnum;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/motherboard/test")
public class TestController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("test")
    public String test() throws JsonProcessingException {
        DeviceQueueMessage deviceQueueMessage = new DeviceQueueMessage();
        deviceQueueMessage.setType(DeviceMessageTypeEnum.HEARTBEAT_CHECK.getType());
        deviceQueueMessage.setPayload("payload");
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend(RabbitMQConstant.IOT_EXCHANGE, RabbitMQConstant.IOT_ROUTEING_KEY, objectMapper.writeValueAsString(deviceQueueMessage));
        return "ok";
    }
}
